﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Net;//新加的
using System.Net.Sockets;//新加的
namespace RxjhServer
{
    public partial class  Form8 : Form
    {
        public static World world;

        string[] kkk;
        public Form8()
        {
            InitializeComponent();
        }
        [STAThread]
        static void Main()
        {
            try
            {
                //Form8 td = new Form8();

                //if (td.qdyzcod())
                //{

                    Application.Run(new Form1());

                /*}
                else
                {
                    Application.Run(new Form8());
                }*/
            }
            catch (Exception ex)
            {
                Environment.Exit(0);
                //MessageBox.Show("Main 错误" + ex);
            }
        }
        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        public static bool AppInstance(double num)
        {
            Process[] MyProcesses = Process.GetProcesses();
            int i = 0;
            foreach (Process MyProcess in MyProcesses)
            {
                if (MyProcess.ProcessName == Process.GetCurrentProcess().ProcessName)
                {
                    i++;
                }
            }
            return (i > num) ? true : false;
        }
        //时间计算

        // 小端存储与大端存储的转换
        private uint swapEndian(ulong x)
        {
            return (uint)(((x & 0x000000ff) << 24) +
            ((x & 0x0000ff00) << 8) +
            ((x & 0x00ff0000) >> 8) +
            ((x & 0xff000000) >> 24));
        }
        public DateTime getWebTime()
        {
            // default ntp server
            const string ntpServer = "ntp1.aliyun.com";

            // NTP message size - 16 bytes of the digest (RFC 2030)
            byte[] ntpData = new byte[48];
            // Setting the Leap Indicator, Version Number and Mode values
            ntpData[0] = 0x1B; // LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)

            IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList;
            //foreach (var item in addresses)
            //{
            //    Debug.WriteLine("IP:" + item);
            //}

            // The UDP port number assigned to NTP is 123
            IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123);

            // NTP uses UDP
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.Connect(ipEndPoint);
            // Stops code hang if NTP is blocked
            socket.ReceiveTimeout = 3000;
            socket.Send(ntpData);
            socket.Receive(ntpData);
            socket.Close();

            // Offset to get to the "Transmit Timestamp" field (time at which the reply 
            // departed the server for the client, in 64-bit timestamp format."
            const byte serverReplyTime = 40;
            // Get the seconds part
            ulong intPart = BitConverter.ToUInt32(ntpData, serverReplyTime);
            // Get the seconds fraction
            ulong fractPart = BitConverter.ToUInt32(ntpData, serverReplyTime + 4);
            // Convert From big-endian to little-endian
            intPart = swapEndian(intPart);
            fractPart = swapEndian(fractPart);
            ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000UL);

            // UTC time
            DateTime webTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds(milliseconds);
            // Local time
            return webTime.ToLocalTime();
        }
        Ini ini = new Ini(Application.StartupPath + "\\config.ini");
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
            

                //world = new World();
                //world.SetConfig();
                //textBox2.Text = World.Key;

                //kkk = textBox2.Text.Split(';');
                //textBox3.Text = World.Key2;
                SyjiaetricMethod md5 = new SyjiaetricMethod();
                string aaa = md5.Encrypto(Hasher.GetMac() + "|" + Hasher.GetIP() + "|" + Hasher.GetDriveID("C") + "|" + Hasher.GetCpuID());
                textBox1.Text = aaa;
                textBox1.Text = textBox1.Text.Trim();
                //if (kkk.Length < 2)
                //{
                //    tishi.Text = "注册码不正确！请重新获取新的注册码输入！";
                //    return;
                //}
             
            }
            catch (Exception)
            {

                Environment.Exit(0);
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //World.Key = textBox2.Text.ToString();
                kkk = textBox2.Text.Split('|');
                if(kkk.Length <2)
                {
                    tishi.Text = "请输入正确注册码！";
                    return;
                }
                World.Key = kkk[0];
                World.Key2 = kkk[1];
                //World.Key2 = textBox3.Text.ToString();
                DbClss.Config.IniWriteValue("GameServer", "Key", kkk[0]);
                DbClss.Config.IniWriteValue("GameServer", "Key2", kkk[1]);
                if (yzcod())
                {
                    Form1 fm2 = new Form1();
                    this.Hide();
                    fm2.ShowDialog();
                    Application.ExitThread();
                }
            }
            catch (Exception)
            {

                Environment.Exit(0);
            }

        }
        public bool qdyzcod()
        {
            try
            {
                SymmetricMethod md5 = new SymmetricMethod();
                string kke = md5.Decrypto(ini.GetString("Key"));
                string[] kk = kke.Split('|');
                if (kk.Length >= 4)
                {
                    if (Hasher.GetCpuID() != World.pWord(kk[0], 2) || Hasher.GetDriveID("C") != World.pWord(kk[1], 2) || Hasher.GetIP() != World.pWord(kk[2], 2) || Hasher.GetMac() != World.pWord(kk[3], 2))
                    {
                        return false;
                    }
                    else
                    {
                        World.Keyk.CpuID = World.pWord(kk[0], 2);
                        World.Keyk.DriveID = World.pWord(kk[1], 2);
                        World.Keyk.IP = World.pWord(kk[2], 2);
                        World.Keyk.Mac = World.pWord(kk[3], 2);
                    }

                }
                else
                {
                    return false;
                }
                Class2 md6 = new Class2();
                string kkf = md6.DecryptByPublicKey(ini.GetString("Key2"));
                string[] jj = kkf.Split('|');
                if (jj.Length >= 4)
                {
                    if (Hasher.GetCpuID() != World.pWord(jj[0], 2) || Hasher.GetIP() != World.pWord(jj[2], 2) || Hasher.GetMac() != World.pWord(jj[3], 2))
                    {
                        return false;
                    }
                    else
                    {

                        World.KeykF.CpuID = World.pWord(jj[0], 2);
                        World.KeykF.DriveID = World.pWord(jj[1], 2);
                        World.KeykF.IP = World.pWord(jj[2], 2);
                        World.KeykF.Mac = World.pWord(jj[3], 2);
                        World.KeykF.SJtime = World.pWord(jj[5], 2);
                        World.KeykF.Numyx = World.pWord(jj[6], 2);
                    }
                }
                else
                {
                    return false;
                }
                ////DateTime start = Convert.ToDateTime(getWebTime());
                ////DateTime end = Convert.ToDateTime(World.KeykF.SJtime);
                //////int Sytime=

                ////TimeSpan sp = end.Subtract(start);
                //////return sp.Days;
                ////World.Gamedayov = sp.Days;
                ////if (sp.Days <= 0)
                ////{
                ////    tishi.Text = "注册码已过期！请重新获取新的注册码输入！";
                ////    return false;
                ////}

                return true;
            }
            catch (Exception)
            {
                //Form1.WriteLine(1, "系统错误:" + exception1.Message);
                //Form1.WriteLine(1, "11" + exception1.Message);
                //Environment.Exit(0);
                tishi.Text = "检查注册码异常！请重新获取新的注册码输入！";
                return false;
            }
        }
        public bool yzcod()
        {
            try
            {
                SymmetricMethod md5 = new SymmetricMethod();
                string kke = md5.Decrypto(World.Key);
                string[] kk = kke.Split('|');
                if (kk.Length >= 4)
                {
                    //DateTime Pktime = DateTime.Now;
                    //Console.WriteLine(Pktime + " " + Pktime.Millisecond);
                    if (Hasher.GetCpuID() != World.pWord(kk[0], 2) || Hasher.GetDriveID("C") != World.pWord(kk[1], 2) || Hasher.GetIP() != World.pWord(kk[2], 2) || Hasher.GetMac() != World.pWord(kk[3], 2))
                    {
                        tishi.Text = "注册码不正确！请重新输入！";
                        return false;
                    }
                    else
                    {
                        World.Keyk.CpuID = World.pWord(kk[0], 2);
                        World.Keyk.DriveID = World.pWord(kk[1], 2);
                        World.Keyk.IP = World.pWord(kk[2], 2);
                        World.Keyk.Mac = World.pWord(kk[3], 2);
                    }

                }
                else
                {
                    tishi.Text = "注册码不正确！请重新输入！";
                    return false;
                }


                Class2 md6 = new Class2();
                //SymmetricMethod md5 = new SymmetricMethod();
                string kkf = md6.DecryptByPublicKey(World.Key2);
                string[] jj = kkf.Split('|');
                if (jj.Length >= 4)
                {
                    if (Hasher.GetCpuID() != World.pWord(jj[0], 2) || Hasher.GetIP() != World.pWord(jj[2], 2) || Hasher.GetMac() != World.pWord(jj[3], 2))
                    {
                        tishi.Text = "注册码不正确！请重新输入！";
                        return false;
                    }
                    else
                    {

                        World.KeykF.CpuID = World.pWord(jj[0], 2);
                        World.KeykF.DriveID = World.pWord(jj[1], 2);
                        World.KeykF.IP = World.pWord(jj[2], 2);
                        World.KeykF.Mac = World.pWord(jj[3], 2);
                        World.KeykF.SJtime = World.pWord(jj[5], 2);
                    }
                }
                else
                {
                    tishi.Text = "注册码不正确！请重新输入！";
                    return false;
                }
                //DateTime start = Convert.ToDateTime(getWebTime());
                //DateTime end = Convert.ToDateTime(World.KeykF.SJtime);
                ////int Sytime=

                //TimeSpan sp = end.Subtract(start);
                ////return sp.Days;
                //World.Gamedayov = sp.Days;
                //if (sp.Days <= 0)
                //{
                //    tishi.Text = "注册码已过期！请重新获取新的注册码输入！";
                //    return false;
                //}

                return true;
            }
            catch (Exception)
            {
                tishi.Text = "检查注册码异常2！请重新获取新的注册码输入！";
                return false;
            }
        }


        private void textBox1_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(textBox1.Text);
            tishi.Text = "注册码已经复制到剪切板！";

            //DialogResult dr = MessageBox.Show("复制成功!请发送给作者机器码!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            //if (dr == DialogResult.OK)
            //{
            //    Clipboard.SetText(textBox1.Text);
            //}
            //else
            //{

            //}
        }
    }
}
